<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\admin\model\TaskImg;
use app\admin\model\User;
use app\admin\model\ProductCategory;

/**
 * 心愿管理
 *
 * @icon   fa fa-list
 */
class Task extends Backend
{

    protected $model = null;
    protected $categorylist = [];
    protected $noNeedRight = ['selectpage'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new TaskImg();
    }

    /**
     * 查看
     */
    public function index()
    {

        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list as $k => $v) {
                $user =  User::where(['id'=>$v['user_id']])->find();
                $list[$k]['nickname'] = $user['nickname'];
            }
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }
    
    //上/下架
    public function setStatus()
    {
        if ($this->request->isPost()) {
            $data = $this->request->request();
            if(!$data['id']){
                $this->error('参数错误');
            }
            $result =  $this->model->where(['id'=>$data['id']])->update(['status'=>$data['status']]);
            if ($result !== false) {
                if($data['status'] == 1){
                    $res = $this->model->where(['id'=>$data['id']])->find();
                    //通过给奖励
                    if($res['type'] = 1){
                        $type = 41;
                        $title = '分享平台闲置到朋友圈';
                    }elseif($res['type'] = 2){
                        $type = 42;
                        $title = '分享平台闲置到微信群';
                    }elseif($res['type'] = 3){
                        $type = 43;
                        $title = '点赞＋评论宝忠置换视频号';
                    }elseif($res['type'] = 4){
                        $type = 44;
                        $title = '点赞＋评论评论宝忠置换抖音号';
                    }
                    roseandlog($res['user_id'],1,$type,$title);
                }
                $this->success();
            } else {
                $this->error('操作失败');
            }
        }
    }

}
